package group2.controller;

import group2.model.Book;
import group2.model.BookType;
import group2.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 薛献军
 * @date 2020/8/18 9:01
 */
@Controller
@RequestMapping("/book")
public class BookController {

    static Logger logger = Logger.getLogger(BookController.class);

    @Autowired
    IBookService bookService;


    /**
     * 关键词搜索
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public List<Book> searchBook(@RequestParam("condition") String condition){
        List<Book> bookList = this.bookService.search(condition);
        System.out.println(condition);
        return bookList;
    }

    /**
     * 查询所有书籍类型信息
     * @return
     */
    @RequestMapping("/type")
    @ResponseBody
    public List<BookType> allBookTypes(){
        List<BookType> bookTypeList = this.bookService.allBookTypes();
        return bookTypeList;
    }

    /**
     * 按书籍号查询书籍详情
     * @param bookId
     * @return
     */
    @RequestMapping("/detail")
    @ResponseBody
    public Book selectByBookId(@RequestParam("bookId") int bookId){
        Book book = this.bookService.selectByBookId(bookId);
        return book;
    }

    /**
     * 分类导航/分页显示
     * @return
     */
    // @RequestMapping("/typelead")
    @GetMapping("typelead")
    @ResponseBody
    public List<Book> selectByTypeId(HttpServletRequest request){

        int typeId = Integer.parseInt(request.getParameter("typeId"));
        List<Book> bookList = this.bookService.selectByTypeId(typeId);
        return bookList;
    }

    /**
     * 新书上架
     */
    @RequestMapping("/new")
    @ResponseBody
    public List<Book> newBooks(){
        List<Book> bookList = this.bookService.newBooks();
        return bookList;
    }

    /**
     * 特价精选，按discount升序排序，选前10个
     * @return
     */
    @RequestMapping("/discount")
    @ResponseBody
    public List<Book> discountBooks(){
        List<Book> bookList = this.bookService.discountBooks();
        return bookList;
    }


    /**
     * 每日好书
     */
    @RequestMapping("/good")
    @ResponseBody
    public List<Book> goodBooks(){
        return this.bookService.goodBooks();
    }

}
